package com.example.item01.controller;

import com.example.item01.proj.Emp;
import com.example.item01.proj.Result;
import com.example.item01.service.EmpService;
import com.example.item01.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result Login(@RequestBody Emp emp) {
        log.info("对象是{}", emp);

        Emp e=empService.login(emp);


        if(e!=null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("gender",e.getGender());

            String re= JwtUtils.generateJwt(claims);
            return Result.success(re);
        }
            return Result.error("用户名/密码错误");

    }


}
